import Cookies from 'js-cookie'
// cookie保存的天数
import config from '@/config'
const { cookieExpires } = config

export const TOKEN_KEY = 'token'

export const setToken = (token) => {
  Cookies.set(TOKEN_KEY, token, { expires: cookieExpires || 1 })
}

export const getToken = () => {
  const token = Cookies.get(TOKEN_KEY)
  if (token) return token
  else return false
}

 /**
  * 获取当前页面url,取消无效内容
  */
 export const getCurrentUrl = ()=>{
  let currentUrl = window.location.href
  let queryIndex = currentUrl.indexOf('?')
    if(queryIndex>0){
      currentUrl = currentUrl.slice(0,queryIndex)
  }    
  return currentUrl;       
 }

 export const getTokenFromURI = ()=>{
  let currentUrl = window.location.href
  let key = "token"
  let tokenIndexOf = currentUrl.indexOf("token") 
  if(tokenIndexOf>0){
    return currentUrl.slice(tokenIndexOf+key.length+1)
  }
  return '';
 }